package com.gsm.system.controller;

import com.gsm.common.comtroller.BaseController;
import com.gsm.common.entity.PageResult;
import com.gsm.common.entity.Result;
import com.gsm.common.entity.ResultCode;
import com.gsm.common.poi.ExcelImportUtil;
import com.gsm.common.utils.JwtUtils;
import com.gsm.common.utils.PermissionConstants;
import com.gsm.domain.system.Permission;
import com.gsm.domain.system.Role;
import com.gsm.domain.system.User;
import com.gsm.domain.system.resp.ProfileResult;
import com.gsm.domain.system.resp.UserResult;
import com.gsm.domain.system.resp.UserSimpleResult;
import com.gsm.system.service.PermissionService;
import com.gsm.system.service.UserService;
import io.jsonwebtoken.Claims;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//1.解决跨域
@CrossOrigin
//2.声明restContoller
@RestController
//3.设置父路径
@RequestMapping(value = "/sys/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private PermissionService permissionService;

    //@RequestMapping(method = RequestMethod.PUT, name = "API-USER-ASSIGNROLES")
    @PutMapping("/assignRoles")
    public Result assignRoles(@RequestBody Map<String, Object> map) {
        //1.获取被分配的用户id
        String userId = (String) map.get("id");
        //2.获取到角色的id列表
        List<String> roleIds = (List<String>) map.get("roleIds");
        //3.调用service完成角色分配
        userService.assignRoles(userId, roleIds);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 保存
     */
    @PostMapping
    public Result save(@RequestBody User user) {
        //1.设置保存的企业id
        user.setCompanyId(companyId);
        user.setCompanyName(companyName);
        //2.调用service完成保存企业
        userService.save(user);
        //3.构造返回结果
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 查询企业的部门列表
     * 指定企业id
     */
    //  @RequestMapping(method = RequestMethod.GET, name = "API-USER-FINDALL")
    @GetMapping
    public Result findAll(int page, int size, @RequestParam Map map) {
        //1.获取当前的企业id
        map.put("companyId", companyId);
        //2.完成查询
        Page<User> pageUser = userService.findAll(map, page, size);
        //3.构造返回结果
        PageResult pageResult = new PageResult(pageUser.getTotalElements(), pageUser.getContent());
        return new Result(ResultCode.SUCCESS, pageResult);
    }

    @GetMapping("/simple")
    public Result simple() throws Exception {
        List<UserSimpleResult> list = new ArrayList<>();
        List<User> users = userService.findAll(companyId);
        for (User user : users) {
            list.add(new UserSimpleResult(user.getId(),user.getUsername()));
        }
        return new Result(ResultCode.SUCCESS,list);
    }

    /**
     * 根据ID查询user
     */
    @GetMapping("/{id}")
    //@RequestMapping(method = RequestMethod.GET, value = "/{id}", name = "API-USER-SELETE")
    public Result findById(@PathVariable String id) {
        User user = userService.findById(id);
        UserResult userResult = new UserResult(user);
        return new Result(ResultCode.SUCCESS, userResult);
    }

    /**
     * 修改User
     */
    //@RequestMapping(method = RequestMethod.PUT, value = "/{id}", name = "API-USER-UPDATE")
    @PutMapping("/{id}")
    public Result update(@PathVariable String id, @RequestBody User user) {
        //1.设置修改的部门id
        user.setId(id);
        //2.调用service更新
        userService.update(user);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id删除
     */
//    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}", name = "API-USER-DELETE")
    @RequiresPermissions(value = "API-USER-DELETE")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        userService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 用户登录
     * 1.通过service根据mobile查询用户
     * 2.比较password
     * 3.生成jwt信息
     */
    @PostMapping(value = "/login")
    public Result login(@RequestBody Map<String, String> loginMap) {
        String mobile = loginMap.get("mobile");
        String password = loginMap.get("password");
        try {
            //1.构造登入的令牌
            password = new Md5Hash(password, mobile, 3).toString();//1.密码，盐 ，加密次数
            UsernamePasswordToken upToken = new UsernamePasswordToken(mobile, password);
            //2.获取subject
            Subject subject = SecurityUtils.getSubject();
            //3.调用login的方法
            subject.login(upToken);
            //4.获取SeesionId
            String sessionId = (String) subject.getSession().getId();
            return new Result(ResultCode.SUCCESS, sessionId);
        } catch (Exception e) {
            return new Result(ResultCode.MOBILEORPASSWORDERROR);
        }

        //5.获取返回结果


//        User user = userService.findByMobile(mobile);
//        //登录失败
//        if (user == null || !user.getPassword().equals(password)) {
//            return new Result(ResultCode.MOBILEORPASSWORDERROR);
//        } else {
//            StringBuilder sb = new StringBuilder();
//            for (Role role : user.getRoles()) {
//                for (Permission perm : role.getPermissions()) {
//                    if (perm.getType() == PermissionConstants.PERMISSION_API){
//                        sb.append(perm.getCode()).append(",");
//                    }
//                }
//            }
//            //登录成功
//            Map<String, Object> map = new HashMap<>();
//            map.put("companyId", user.getCompanyId());
//            map.put("companyName", user.getCompanyName());
//            map.put("apis",sb.toString());
//            String token = jwtUtils.createJwt(user.getId(), user.getUsername(), map);
//            return new Result(ResultCode.SUCCESS, token);

    }


    /**
     * 用户登录成功之后，获取用户信息
     * 1.获取用户id
     * 2.根据用户id查询用户
     * 3.构建返回值对象
     * 4.响应
     */
//    @RequestMapping(value = "/profile", method = RequestMethod.POST)
    @PostMapping("/profile")
    public Result profile(HttpServletRequest request) throws Exception {
        Subject subject = SecurityUtils.getSubject();
        //1.获取Subject的所有安全数据集合
        PrincipalCollection principals = subject.getPrincipals();
        ProfileResult result =  (ProfileResult)principals.getPrimaryPrincipal();

//        Claims claims = (Claims) request.getAttribute("user_claims");
//        String userid = claims.getId();
//        User user = userService.findById(userid);
//        //根据不同的用户级别 获取用户应用权限
//
//        ProfileResult result = null;
//        if ("companyUser".equals(user.getLevel())) {
//            result = new ProfileResult(user);
//        } else {
//            Map map = new HashMap();
//            if ("companyAdmin".equals(user.getLevel())) {
//                map.put("enVisible", "1");
//            }
//            List list = permissionService.findAll(map);
//            result = new ProfileResult(user, list);
//        }
        return new Result(ResultCode.SUCCESS, result);
    }
    /**
     * 导入Excel，添加用户
     *  文件上传：springboot
     */
    @PostMapping("/import")
    public Result importUser(@RequestParam(name="file") MultipartFile file) throws Exception {
        //1.解析Excel
//        //1.1.根据Excel文件创建工作簿
//        Workbook wb = new XSSFWorkbook(file.getInputStream());
//        //1.2.获取Sheet
//        Sheet sheet = wb.getSheetAt(0);//参数：索引
//        //1.3.获取Sheet中的每一行，和每一个单元格
//        //2.获取用户数据列表
//        List<User> list = new ArrayList<>();
//        System.out.println(sheet.getLastRowNum());
//        for (int rowNum = 1; rowNum<= sheet.getLastRowNum() ;rowNum ++) {
//            Row row = sheet.getRow(rowNum);//根据索引获取每一个行
//            Object [] values = new Object[row.getLastCellNum()];
//            for(int cellNum=1;cellNum< row.getLastCellNum(); cellNum ++) {
//                Cell cell = row.getCell(cellNum);
//                Object value = getCellValue(cell);
//                values[cellNum] = value;
//            }
//            User user = new User(values);
//            list.add(user);
//        }

        List<User> list = new ExcelImportUtil(User.class).readExcel(file.getInputStream(), 1, 1);
        //3.批量保存用户
        userService.saveAll(list,companyId,companyName);

        return new Result(ResultCode.SUCCESS);
    }

    public static Object getCellValue(Cell cell) {
        //1.获取到单元格的属性类型
        CellType cellType = cell.getCellType();
        //2.根据单元格数据类型获取数据
        Object value = null;
        switch (cellType) {
            case STRING:
                value = cell.getStringCellValue();
                break;
            case BOOLEAN:
                value = cell.getBooleanCellValue();
                break;
            case NUMERIC:
                if(DateUtil.isCellDateFormatted(cell)) {
                    //日期格式
                    value = cell.getDateCellValue();
                }else{
                    //数字
                    value = cell.getNumericCellValue();
                }
                break;
            case FORMULA: //公式
                value = cell.getCellFormula();
                break;
            default:
                break;
        }
        return value;
    }
}